package com.xiaomage.sax.demo

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
import javax.xml.parsers.SAXParser
import javax.xml.parsers.SAXParserFactory

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        button.setOnClickListener{
            //获取资源对象
            val inputStream=assets.open("person.xml")
            //创建XML  SAX解析处理器
            val saxHandler=SAXHandler()
            //获取SAX解析工厂实例
            val saxParserFactory= SAXParserFactory.newInstance()
            //创建XML SAX解析器
            val saxParaser= saxParserFactory.newSAXParser()
            //把解析资源配置给解析器，同时将解析处理器配置给解析器
            saxParaser.parse(inputStream,saxHandler)
            //关闭流
            inputStream.close()
            var result="";
            for(person in saxHandler.persons){
                result+="/n"+person.toString()
            }
            tv.text=result
        }
    }
}
